package com.bjc.sdx.fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import androidx.fragment.app.Fragment;

import com.bjc.sdx.MainActivity;
import com.bjc.sdx.R;

public class Fragment2 extends Fragment {

    private SeekBar sb_normal;
    private TextView txt_cur;
    private Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container,false);
        // 布局中的Spinner的事件监听
        Spinner spinner = view.findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] languages = getResources().getStringArray(R.array.languages);
                Toast.makeText(getActivity(), "你点击的是" +languages[i], Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // Another interface callback
            }
        });

        // 布局中的AutoCompleteTextView的事件监听
        AutoCompleteTextView auto = view.findViewById(R.id.auto);
        // 1.准备数据源
        String[] countrys = getResources().getStringArray(R.array.country);
        // 2.将数据源数据加载到适配器中
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,
                countrys);
        // 3.将适配器数据加载到控件中
        auto.setAdapter(adapter);
        // 表示当AutoCompleteTextView控件中某一项被点击的监听事件
        auto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 根据下标获取点击item的信息
                String str = adapterView.getItemAtPosition(i).toString();
                Toast.makeText(getActivity(),"当前选中了"+str,Toast.LENGTH_SHORT).show();
            }
        });

        mContext = getActivity();
        bindViews(view);
        return view;
    }

    private void bindViews(View view) {
        sb_normal = (SeekBar) view.findViewById(R.id.sb_normal);
        txt_cur = (TextView) view.findViewById(R.id.txt_cur);
        sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txt_cur.setTextSize(30);
                txt_cur.setFocusable(true);
                txt_cur.setFocusableInTouchMode(true);
                txt_cur.setClickable(true);
                txt_cur.setBackgroundResource(R.color.white);
                txt_cur.setSingleLine(true);
                txt_cur.setHorizontallyScrolling(true);
                txt_cur.setEllipsize(TextUtils.TruncateAt.MARQUEE);
                txt_cur.setMarqueeRepeatLimit(6);
                txt_cur.setGravity(View.TEXT_ALIGNMENT_CENTER);
                txt_cur.setBackgroundColor(Color.argb(0, 0, 0, 0));
                txt_cur.setShadowLayer(3,10, 10, R.color.teal_200);
                txt_cur.setText("当前进度值:" + progress + "  / 100 ");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
